﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Core
{
    public partial class User
    {
        public static Guid? CurrentUserId
        {
            get
            {
                MembershipUser user = Membership.GetUser();
                if (user != null)
                    return (Guid)user.ProviderUserKey;
                return null;
                    
            }
        }

        public static IQueryable<ViewUser> LoadSearchResult(string userName, string name)
        {
            DataAccessDataContext db = new DataAccessDataContext();
            IQueryable<ViewUser> baseQuery = from u in db.ViewUsers
                                             select u;
            if (!string.IsNullOrEmpty(userName))
                baseQuery = from u in baseQuery
                            where u.UserName.Contains(userName)
                            select u;
            if (!string.IsNullOrEmpty(name))
                baseQuery = from u in baseQuery
                            where u.Shortname.Contains(name)
                            select u;
            return baseQuery;
        }

        public Guid RoleId
        {
            get
            {
                DataAccessDataContext db = new DataAccessDataContext();
                return (from u in db.Aspnet_UsersInRoles
                        where u.UserId == this.Id
                        select u.RoleId).First();
            }
        }

        public void SetRoleId(Guid roleId, DataAccessDataContext db)
        {
            db.Aspnet_UsersInRoles.DeleteAllOnSubmit(
                from ur in db.Aspnet_UsersInRoles
                where ur.UserId == this.Id
                select ur);
            db.Aspnet_UsersInRoles.InsertOnSubmit(new Aspnet_UsersInRole() { RoleId = roleId, UserId = this.Id });
            db.SubmitChanges();
        }

        public static User[] GetAllUsers()
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var result = from user in dc.Users
                             select user;
                return result.ToArray<User>();
            }
        }

        public static void AddUser(User user)
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                dc.Users.InsertOnSubmit(user);
                dc.SubmitChanges();
            }
        }

        public static User GetUserByEmail(String Email)
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var result = from user in dc.Users
                             where user.Email == Email
                             select user;
                return result.Single<User>();
            }
        }

        public static bool BelongsToDepartmentLevel(DAL.DataAccessLayer dc, String user, Guid department_level_id)
        {
            try
            {
                var query = from ud in dc.DataContext.UserDepartments
                            where ud.Id.Equals(department_level_id) && ud.UserId.Equals(user)
                            select ud;

                UserDepartment u = query.First();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static IEnumerable<UserDepartment> GetDepartmentLevels(DAL.DataAccessLayer dc, Guid userid)
        {
            var query = from ud in dc.DataContext.UserDepartments
                        where ud.User.Id.Equals(userid)
                        select ud;

            return query.ToArray<UserDepartment>();
        }
    }
}
